package com.haipumi.servlets.backadmin;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.haipumi.beans.haipumi_product;
import com.haipumi.biz.GoodsBiz;
import com.haipumi.biz.impl.GoodsBizImpl;
import com.haipumi.servlets.CommonServlet;
import com.haipumi.utils.CommonData;
import com.haipumi.utils.RequestUtil;
import com.haipumi.utils.YcStringUtils;

public class BackGoodsServlet extends CommonServlet {

	private GoodsBiz gb=new GoodsBizImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op=request.getParameter("op");
		String pid=request.getParameter("pid");
		if(op!=null){
			if("findGoods".equals(op)){
				findGoodsOp(request,response);
			}else if("addGoods".equals(op)){
				addGoodsOp(request,response);
			}else if("updateGoods".equals(op)){
				updateGoodsOp(request,response);
			}else if("updateGoodsDetail".equals(op)){
				updateGoodsDetailOp(request,response);
			}else if("changeStatus".equals(op)){
				changeStatusOp(request,response);
			}
		}
	}
	
	
	//上下架操作
	private void changeStatusOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
		goods=gb.updateGoodsStatus(goods);
		List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
		for(int i=0;i<list.size();i++){
			if(list.get(i).getPid()==goods.getPid()){
				list.get(i).setPstatus(goods.getPstatus());//修改详情
				break;
			}
		}
		request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
	}

	//更新奖品描述
	private void updateGoodsDetailOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
		//对详情信息进行js过滤
		goods.setPdescribe( YcStringUtils.javascriptEscape(goods.getPdescribe())  );
		goods =gb.updateGoodsDetail(goods);
		List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
		for(int i=0;i<list.size();i++){
			if(list.get(i).getPid()==goods.getPid()){
				list.get(i).setPdescribe(goods.getPdescribe());//修改详情
				break;
			}
		}
		request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
		super.outData(response, goods);
	}

	private void updateGoodsOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
		try {
			goods =gb.updateGoods(goods);
			List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
			for(int i=0;i<list.size();i++){
				if(list.get(i).getPid()==goods.getPid()){
					list.set(i, goods);
					break;
				}
			}
			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
			super.outData(response, goods);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void addGoodsOp(HttpServletRequest request, HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
		try {
			//添加goods,并且修改application
			goods=gb.addGoods(goods);
			List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
			list.add(goods);
			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
			super.outData(response, goods);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void findGoodsOp(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		String pname="";
		if(request.getParameter("pname")!=null && !"".equals(request.getParameter("pname")) ){
			pname=request.getParameter("pname");
		}
		int total=gb.findGoodsCount(pname);
		List<haipumi_product> list=gb.findGoods(pname,rows, page, sort, order);
		//因为easyui的datagrid 控件要求回传的数据格式必须是这样的 rows:[] ,total:xx
		DataGridModel dgm=new DataGridModel();
		dgm.setRows(list);
		dgm.setTotal(total);
		super.outData(response, dgm);
	}

}
